<?php
namespace App\Library\Sms;

use App\Repositories\SmsCodeRespository;
use App\Services\AliService;
use App\Traits\ReturnTrait;
use Hyperf\Di\Annotation\Inject;

class AliSms extends AbstractSms implements SmsInterface{
    /**
     * @Inject
     * @var AliService
     */
    protected $ali_service;
    use ReturnTrait;
    public function send($data){
        $code=null;
        $model_data=[];
        $expire_time = 0;
        if($data['code']){
            $code = $this->createCode();
            $expire_time = time()+15*60;
        }
        $model_data=[
            'mobile' => $data['mobile'],
            'type' => $data['type'],
            'code' => $code,
            'text'=>'',
            'expire_time' => $expire_time
        ];
        $return  = SmsCodeRespository::getInstance()->store($model_data);
        if($return['success']){
            $result=$this->ali_service->sendSms([
                'mobile' => $data['mobile'],
                'template' => config('ali.TemplateCode'),
                'template_params' => $data['code']?json_encode(['code'=>$code]):json_encode([])
            ]);
            if($result->body->code=='OK'){
                return $this->success([]);
            }
            return $this->error($result->body->message);
            //执行发送短信操作

        }
        return $this->error('短信保存失败');
    }
}
